<?php
namespace app\admin\controller;

use app\common\model\AdminConfig as AdminConfigModel;
use app\common\controller\AdminBase;
use think\Cache;
use think\Db;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class Config extends AdminBase
{
	private $option_info =[
				"text" => "单行文本",
				"textarea" => "多行文本",
				"static" => "静态文本",
				"password" => "密码",
				"checkbox" => "复选框",
				"radio" => "单选按钮",
				"date" => "日期",
				"datetime" => "日期+时间",
				"hidden" => "隐藏",
				"switch" => "开关",
				"array" => "数组",
				"select" => "下拉框",
				"linkage" => "普通联动下拉框",
				"linkages" => "快速联动下拉框",
				"image" => "单张图片",
				"images" => "多张图片",
				"file" => "单个文件",
				"files" => "多个文件",
				"ueditor" => "编辑器",
				"icon" => "字体图标",
				"tags" => "标签",
				"number" => "数字",
				"masked" => "格式文本",
				"range" => "范围",
				"time" => "时间",
	];
    public function initialize()
    {
        parent::initialize();
		$this->aconfig_model = new AdminConfigModel();
    }

    /**
     * 表单
     */
    public function index()
    {
		$user_list = '';
		$keyword = '';
		return $this->fetch('index', ['user_list' => $user_list, 'keyword' => $keyword]);
    }
	
    /*
     * 获取表单数据
     */
    public function getinfo($group='base',$page = 1,$limit=10,$keyword=false)
    {

		$map =[];
		$map['group'] = $group;
		if($keyword){
			$map["name|title"] = ['like','%'.$keyword.'%'];
		}
		$info_list = $this->aconfig_model->where($map)->limit($limit)->page($page,$limit)->select();

		$return = [
			'code' => 0,
			'msg'  => '',
			'count'=> count($info_list),
			'data' => $info_list
		];
		return json($return);
    }
	
    /*
     * 新增表单数据
     */
    public function add()
    {
			$type   = $this->request->get('type');
			return $this->fetch('add', ['type' => $type,'option_info'=>$this->option_info]);
    }
	
    /*
     * 保存表单数据
     */
    public function save()
    {
			$data    = $this->request->post();
			$results = $this->aconfig_model->allowField(true)->save($data);
			if ($results) {
                   $this->success('添加成功');
			}else {
                   $this->error('添加失败');
            }
    }
	
    /*
     * 编辑表单数据(view)
     */
    public function edit()
    {
			$data   = $this->request->get();
			if (empty($data['id'])) {
                $this->error('请输入正确的ID');
            } else {
				$sinfo 	= 	AdminConfigModel::get($data['id']);
				return $this->fetch('edit', ['config_info' => $sinfo,'option_info'=>$this->option_info]);
            }
    }

	
    /*
     * 更新表单数据
     */
    public function update()
    {
		if ($this->request->isPost()) {
			$data   = $this->request->post();
			if (empty($data['id'])) {
                $this->error('请输入正确的ID');
            } else {
				$sinfo 	= 	AdminConfigModel::get($data['id']);
                if ($sinfo->save($data)) {
                    $this->success('编辑成功');
				}else {
                    $this->error('编辑失败');
                }
            }
		}
    }

    /*
     * 改变表单数据状态
     */
    public function change()
    {
			$data   = $this->request->get();
			if ( !empty($data['id']) && is_numeric($data['status'])) {
				$sinfo  = AdminConfigModel::where(array('id'=>array('in',$data['id'])));
                if ($sinfo->update(['status' => $data['status']])) {
                    $this->success('更新成功');
				}else {
                    $this->error('更新失败');
                }
            }
    }
	
    /*
     * 删除表单数据
     */
    public function delete()
    {
        if (AdminConfigModel::destroy(input('id'))) {
            $this->success('删除成功');
		}else {
            $this->error('删除失败');
        }
    }
	


    /**
     * 清除缓存
     */
    public function clear()
    {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
}